import random
from tool import mns_event


class Table:
    table = {}

    @classmethod
    def write(cls, table):
        mns_event.MnsEvent.update_router_table_event.clear()
        if table:
            cls.table = table
        mns_event.MnsEvent.update_router_table_event.set()

    @classmethod
    def read(cls, key):
        if key in cls.table:
            return cls.table[key]

    @classmethod
    def choice_ship_by_random(cls, my_name):
        res_name = random.choice(list(cls.table.keys()))
        while res_name != my_name:
            return res_name
